﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Copad.BusinessCard.DAL;
using Copad.BusinessCard.Model;

namespace Copad.BusinessCard.BLL
{
    public class UserBLL : BLL<User>
    {
        const string SYSTEM_USERNAME = "系统管理员";

        public UserInfo ValidateUser(string username, string userPassword)
        {
            var user = dataBase.Select<User>(x => x.Username == username && x.Password == userPassword && x.IsActive == true).FirstOrDefault();
            if (user == null) { return null; }
            UserInfo userInfo = new UserInfo();
            userInfo.User = user;
            var userGroup = dataBase.Select<UserGroup>(x => x.UID == user.ID);
            userInfo.Groups = new List<Group>();
            var gid = (from ff in userGroup select ff.GID).ToArray();
            userInfo.Groups = dataBase.Select<Group>(w => gid.Contains(w.ID));
            userInfo.GroupsID = string.Join(",", from f in userInfo.Groups select f.ID);
            return userInfo;
        }

        public void Insert(UserInfo user)
        {
            dataBase.BeginTransaction();
            dataBase.Insert(user.User);
            user.Groups.ForEach(x =>
            {
                UserGroup ug = new UserGroup { UID = user.User.ID, GID = x.ID };
                dataBase.Insert(ug);
            });
            dataBase.EndTransaction();
        }

        public new void Delete(long id)
        {
            dataBase.BeginTransaction();
            dataBase.Delete<UserGroup>(x => x.UID == id);
            dataBase.Delete<User>(x => x.ID == id);
            dataBase.EndTransaction();
        }

        public override void Delete(User model)
        {
            Delete(model.ID);
        }

        public List<UserInfo> GetAllUser()
        {
            var list = dataBase.Select<User>();
            List<UserInfo> userInfos = new List<UserInfo>();
            list.ForEach(x =>
            {
                UserInfo ui = new UserInfo { User = x, Groups = new List<Group>() };
                var gid = (from ff in dataBase.Select<UserGroup>(w => w.UID == x.ID) select ff.GID).ToArray();
                ui.Groups = dataBase.Select<Group>(w => gid.Contains(w.ID));
                ui.GroupsID = string.Join(",", from f in ui.Groups select f.ID);

                userInfos.Add(ui);
            });
            return userInfos;
        }

        public void Update(UserInfo userInfo)
        {
            dataBase.BeginTransaction();
            dataBase.Update(userInfo.User);
            dataBase.Delete<UserGroup>(x => x.UID == userInfo.User.ID);
            userInfo.Groups.ForEach(x =>
            {
                UserGroup ug = new UserGroup { UID = userInfo.User.ID, GID = x.ID };
                dataBase.Insert(ug);
            });
            dataBase.EndTransaction();
        }

        public bool IsExist(long id, string username)
        {
            return dataBase.Count<User>(x => x.Username == username && x.ID != id) > 0;
        }
        public bool IsExist(string username)
        {
            return dataBase.Count<User>(x => x.Username == username) > 0;
        }

        public bool IsSystemUser(long id)
        {
            var groups = dataBase.Select<UserGroup>(x => x.UID == id);
            foreach (var x in groups)
            {
                var group = dataBase.Select<Group>(w => w.ID == x.GID).FirstOrDefault();
                if (group != null)
                {
                    if (group.Name == SYSTEM_USERNAME)
                    {
                        return true;
                    }
                }
            }
            return false;
        }


        public List<UserView> GetCardPermissions(long cardID)
        {
            List<UserView> list = new List<UserView>();
            var groupList = Get();
            groupList.ForEach(x =>
            {
                if (!IsSystemUser(x.ID))
                {
                    UserView view = new UserView { ID = x.ID, Username = x.Username, IsSelected = dataBase.Count<CardUserPermissions>(w => w.CardID == cardID && w.UserID == x.ID) > 0 };
                    list.Add(view);
                }
            });
            return list;
        }
    }
}
